home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************/
- /* */
- /* Application: CollectPictColors */
- /* */
- /* Description: This application uses the Picture Utilities package */
- /* and Jon Zap's KnowsPict application to demonstrate */
- /* two methods of collecting colors used by Pict */
- /* resources. In this program, you'll see different */
- /* results for each method. With the Pict Util package, */
- /* the routine, GetPictInfo, returns a colortable with */
- /* the number of colors requested, but only the colors */
- /* used in the picture or it's pixmap(s) image data are */
- /* stored in the requested colors. As for the */
- /* remaining requested colors not used by the picture */
- /* but stored in the picture's pixmap(s) colortable, */
- /* they are set to black. In Jon's application this */
- /* doesn't occur. All the colors used by the picture, */
- /* including those stored in the picture's pixmap(s) */
- /* colortable are returned. To use Jon's routines, */
- /* simply call CollectColors with the appropriate */
- /* parameters. */
- /* */
- /* Files: CollectPictColors.π */
- /* CollectPictColors.c */
- /* CollectPictColors.π.rsrc */
- /* CLUTBuilder.c (written by Jon Zap) */
- /* */
- /* Programmer: Edgar Lee */
- /* Organization: Apple Computer, Inc. */
- /* Department: Developer Technical Support, DTS */
- /* Language: C (Think C version 5.0.1) */
- /* Date Created: 02-20-92 */
- /* */
- /****************************************************************************/
-
- /* Constant Declarations */
-
- #define WWIDTH 470
- #define WHEIGHT 330
-
- #define WLEFT (((screenBits.bounds.right - screenBits.bounds.left) - WWIDTH) / 2)
- #define WTOP (((screenBits.bounds.bottom - screenBits.bounds.top) - WHEIGHT) / 2)
-
- void initMac();
- void createWindow();
- void drawPictureColors();
- void drawColors();
- void doEventLoop();
-
- main()
- {
- initMac();
-
- createWindow();
-
- doEventLoop();
- }
-
- void initMac()
- {
- MaxApplZone();
-
- InitGraf( &thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( nil );
- InitCursor();
- FlushEvents( 0, everyEvent );
- }
-
- void createWindow()
- {
- Rect rect;
- WindowPtr window;
-
- SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
- window = NewCWindow( 0L, &rect, "\pCollect Pict Colors", true, documentProc,
- (WindowPtr)-1L, true, 0L );
- SetPort( window );
-
- TextFont( geneva );
- TextSize( 9 );
- TextMode( srcCopy );
- }
-
- void drawPictureColors()
- {
- Rect rect;
- PicHandle pict;
- CTabHandle ctable;
- short depth, directFlag;
- PictInfo thePictInfo;
- CTabHandle CollectColors();
-
- /* Load the pict resource. */
- pict = (PicHandle)GetResource( 'PICT', 128 );
-
- /* Make sure it's not purgeable. */
- HNoPurge( pict );
-
- /* Set a black background. */
- SetRect( &rect, 0, 0, WWIDTH, WHEIGHT );
- ForeColor( blackColor );
- PaintRect( &rect );
-
- /* See what CollectColors returns. */
- ctable = CollectColors( pict, &depth, &directFlag );
- drawColors( ctable, 20, "\pCOLLECTCOLORS Colortable" );
-
- /* Draw the picture. */
- rect = (**pict).picFrame;
- OffsetRect( &rect, -rect.left + 160, -rect.top + 70 );
- DrawPicture( pict, &rect );
-
- /* Frame the picture. */
- InsetRect( &rect, -2, -2 );
- FrameRect( &rect );
-
- /* Now see what GetPictInfo returns. */
- GetPictInfo( pict, &thePictInfo, returnColorTable, 256, medianMethod, 0 );
- drawColors( thePictInfo.theColorTable, rect.right + 15, "\pGETPICTINFO Colortable" );
-
- /* Release the resource. */
- ReleaseResource( pict );
-
- /* Release the colortable. */
- DisposCTable( ctable );
- }
-
- void drawColors( ctable, offset, string )
- CTabHandle ctable;
- int offset;
- Str255 string;
- {
- int i;
- int col, row;
- Rect rect;
-
- ForeColor( whiteColor );
- BackColor( blackColor );
-
- MoveTo( offset, 20 );
- DrawString( string );
-
- if (ctable != nil)
- {
- for (i = 0; i < 256; i++)
- {
- col = offset + ((i % 8) * 16);
- row = 30 + ((i / 8) * 9);
-
- SetRect( &rect, col, row, col + 10, row + 3 );
- RGBForeColor( &(**ctable).ctTable[i].rgb );
- PaintRect( &rect );
-
- ForeColor( whiteColor );
- InsetRect( &rect, -2, -2 );
- FrameRect( &rect );
- }
- }
- else
- {
- MoveTo( 12, offset );
- DrawString( "\p-Sorry could not load PICT resource." );
- }
- }
-
- void doEventLoop()
- {
- EventRecord event;
- WindowPtr window;
- short clickArea;
- Rect screenRect;
-
- for (;;)
- {
- if (WaitNextEvent( everyEvent, &event, 0, nil ))
- {
- if (event.what == mouseDown)
- {
- clickArea = FindWindow( event.where, &window );
-
- if (clickArea == inDrag)
- {
- screenRect = (**GetGrayRgn()).rgnBBox;
- DragWindow( window, event.where, &screenRect );
- }
- else if (clickArea == inContent)
- {
- if (window != FrontWindow())
- SelectWindow( window );
- }
- else if (clickArea == inGoAway)
- if (TrackGoAway( window, event.where ))
- return;
- }
- else if (event.what == updateEvt)
- {
- window = (WindowPtr)event.message;
- SetPort( window );
-
- BeginUpdate( window );
- drawPictureColors();
- EndUpdate( window );
- }
- }
- }
- }